library(here)
library(data.table)
library(stringr)
library(ggplot2)

load("meeting_replication.RData")

#########################################################################
##Replication Materials (Decision-Making Patterns) ######################
##The Institutional Origins of China's International Crises #############
#########################################################################

#########################################
####DESCRIPTIVE STAISTICS################
#########################################

##Mao Era (Before Death on September 9, 1976)
d1 <- d[d$year<=1976 | (d$year==1976 & d$month<9) | (d$year==1976 & d$month==9 & d$day<=9),]
##Only Meetings In Which Mao Participated
d1 <- d1[d1$leader==1,]

##Add Count Variable
d1$meeting <- 1

##Reformat Variables Names
d1$body_name[d1$body_name=="Informal"] <- "informal"
d1$attendee[d1$attendee=="Both"] <- "both"
d1$attendee[d1$attendee=="Military Only"] <- "military only"
d1$attendee[d1$attendee=="Diplomat Only"] <- "diplomat only"
d1$attendee[d1$attendee=="Neither"] <- "neither"

#########################################
####Generate Figure 1####################
#########################################

##Figure 1 (Panel A)
row.counts1 <- as.data.table(table(d1$body_name, d1$year))
row.counts1$period[row.counts1$V2<1958] <- "pre-Great Leap Forward"
row.counts1$period[row.counts1$V2>=1958 & row.counts1$V2<=1962] <- "post-Great Leap Forward"
row.counts1$period[row.counts1$V2>=1963 & row.counts1$V2<=1965] <- "pre-Cultural Revolution"
row.counts1$period[row.counts1$V2>=1966 & row.counts1$V2<= 1969] <- "early Cultural Revolution"
row.counts1$period[row.counts1$V2>=1970 & row.counts1$V2<= 1976] <- "late Cultural Revolution"

row.avg1 <- row.counts1[ , list(avg = mean(N)), by = list(period, V1)]
row.avg1$period <- as.factor(row.avg1$period)
row.avg1$period <- relevel(row.avg1$period, ref="early Cultural Revolution")
row.avg1$period <- relevel(row.avg1$period, ref="pre-Cultural Revolution")
row.avg1$period <- relevel(row.avg1$period, ref="post-Great Leap Forward")
row.avg1$period <- relevel(row.avg1$period, ref="pre-Great Leap Forward")

##Spell Out CMC/CSC for Graph
row.avg1$V1 <- as.factor(row.avg1$V1)
row.avg1$V1 <- relevel(row.avg1$V1, ref="informal")
row.avg1$V1 <- relevel(row.avg1$V1, ref="Politburo")

plot1 <- ggplot(data=row.avg1, aes(x=period, y=avg, fill=V1)) +
  geom_bar(stat="identity", color="black", position=position_dodge()) +
  theme_minimal() + xlab("") + ylab("Average number of meetings per year \n attended by Mao Zedong") +
  scale_fill_brewer(palette="Greys") + labs(fill = "") + ggtitle("Mao's decision-making venue") +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 9)) +
  theme(legend.position="bottom", legend.margin=margin(-12.5,0,0,0),
        plot.title = element_text(hjust = 0.5))

ggsave("Figure 1 (panel A).pdf", plot = plot1, scale = 1, width = 4.8, height = 4.8, dpi = 500, limitsize = T)

##Figure 1 (Panel B)
row.counts2 <- as.data.table(table(d1$attendee, d1$year))
row.counts2$period[row.counts2$V2<1958] <- "pre-Great Leap Forward"
row.counts2$period[row.counts2$V2>=1958 & row.counts2$V2<=1962] <- "post-Great Leap Forward"
row.counts2$period[row.counts2$V2>=1963 & row.counts2$V2<=1965] <- "pre-Cultural Revolution"
row.counts2$period[row.counts2$V2>=1966 & row.counts2$V2<= 1969] <- "early Cultural Revolution"
row.counts2$period[row.counts2$V2>=1970 & row.counts2$V2<= 1976] <- "late Cultural Revolution"

row.avg2 <- row.counts2[ , list(avg = mean(N)), by = list(period, V1)]
row.avg2$period <- as.factor(row.avg2$period)
row.avg2$period <- relevel(row.avg2$period, ref="early Cultural Revolution")
row.avg2$period <- relevel(row.avg2$period, ref="pre-Cultural Revolution")
row.avg2$period <- relevel(row.avg2$period, ref="post-Great Leap Forward")
row.avg2$period <- relevel(row.avg2$period, ref="pre-Great Leap Forward")

plot2 <- ggplot(data=row.avg2, aes(x=period, y=avg, fill=V1)) +
  geom_bar(stat="identity", color="black", position=position_dodge()) +
  theme_minimal() + xlab("") + ylab("") + 
  theme_minimal() + xlab("") + ylab("Average number of meetings per year \n attended by Mao Zedong") + 
  scale_fill_brewer(palette="Greys") + labs(fill = "") +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 9)) + ggtitle("Bureaucratic participation in Mao's decision-making") + 
  theme(legend.position="bottom", legend.margin=margin(-12.5,0,0,0),
        plot.title = element_text(hjust = 0.5))

ggsave("Figure 1 (panel B).pdf", plot = plot2, scale = 1, width = 4.8, height = 4.8, dpi = 500, limitsize = T)

#########################################
####MAIN TEXT DISCUSSION#################
#########################################

##Politburo Utilization
mao_polit <- nrow(d[d$body_name=="Politburo" & d$leader==1 & d$year>=1950 & d$year<=1962,])
precr_polit <- nrow(d[d$body_name=="Politburo" & d$leader==1 & d$year>=1963 & d$year<=1965,])
cr_polit <- nrow(d[d$body_name=="Politburo" & d$leader==1 & d$year>=1966 & d$year<=1976,])
afterdeath_polit <- nrow(d[d$body_name=="Politburo" & d$leader==1 & d$year>=1977 & d$year<=1981,])
deng_polit <- nrow(d[d$body_name=="Politburo" & d$leader==1 & d$year>=1982 & d$year<=1989,])
round(mao_polit / length(c(1950:1962))) #10 per year
round(precr_polit / length(c(1963:1965))) #11 per year
round(cr_polit / length(c(1966:1976))) #1 per year
round(afterdeath_polit / length(c(1977:1981))) #3 per year
round(deng_polit / length(c(1982:1989))) #1 per year

##Information Provision during Politburo Meetings
round(nrow(d[d$body_name=="Politburo" & d$information==1 & d$leader==1 & d$year>=1950 & d$year<=1962,]) / nrow(d[d$body_name=="Politburo" & d$leader==1 & d$year>=1950 & d$year<=1962,]),2)
#41 percent

##Frequency of General Politburo Meetings Before 1963
nrow(d[d$body_name=="Politburo" & d$type!=2 & d$year<=1962 & d$leader==1,]) #94
nrow(d[d$body_name=="Politburo" & d$year<=1962 & d$leader==1,]) #136
round(94/136,2) #69 percent

##Frequency of Polibturo Standing Committee Meetings After 1963
nrow(d[d$body_name=="Politburo" & d$type==2 & d$year>=1963 & d$year<=1965 & d$leader==1,]) #25
nrow(d[d$body_name=="Politburo" & d$year>=1963 & d$year<=1965 & d$leader==1,]) #32
round(25/32,2) #78 percent

##Central Military Commission Meeting Frequency
mao_cmc <- nrow(d[d$body_name=="CMC/CSC" & d$year>=1950 & d$year<=1962,])
precr_cmc <- nrow(d[d$body_name=="CMC/CSC" & d$year>=1963 & d$year<=1965,])
cr_cmc <- nrow(d[d$body_name=="CMC/CSC" & d$year>=1966 & d$year<=1976,])
afterdeath_cmc <- nrow(d[d$body_name=="CMC/CSC" & d$year>=1977 & d$year<=1981,])
deng_cmc <- nrow(d[d$body_name=="CMC/CSC" & d$year>=1982 & d$year<=1989,])
round(mao_cmc / length(c(1950:1962))) #25 per year
round(precr_cmc / length(c(1963:1965))) #12 per year
round(cr_cmc / length(c(1966:1976))) #17 per year
round(afterdeath_cmc / length(c(1977:1981))) #4 per year
round(deng_cmc / length(c(1982:1989))) #12 per year

##Secretariat
mao_sec <- nrow(d[d$body_name=="Secretariat" & d$year>=1956 & d$year<=1962,])
precr_sec <- nrow(d[d$body_name=="Secretariat" & d$year>=1963 & d$year<=1965,])
cr_sec <- nrow(d[d$body_name=="Secretariat" & d$year>=1966 & d$year<=1976,])
round(mao_sec / length(c(1956:1962))) #10 per year
round(precr_sec / length(c(1963:1965))) #3 per year
round(cr_sec / length(c(1966:1976))) #0 per year

##State Council
mao_sc <- nrow(d[d$body_name=="State Council" & d$year>=1953 & d$year<=1962,])
precr_sc <- nrow(d[d$body_name=="State Council" & d$year>=1963 & d$year<=1965,])
cr_sc <- nrow(d[d$body_name=="State Council" & d$year>=1966 & d$year<=1976,])
round(mao_sc / length(c(1953:1962)), ) #4 per year
round(precr_sc / length(c(1963:1965))) #2 per year
round(cr_sc / length(c(1966:1976))) #1 per year

##Defense/Diplomatic Participation in Coordination Meetings during Early Mao Era
nrow(d[d$coordinator==1 & d$year>=1950 & d$year<=1962 & d$attendee=="Both",]) #317
nrow(d[d$coordinator==1 & d$year>=1950 & d$year<=1962,]) #550
317/550

##Foreign Affairs Leading Small Group
deng_falsg <- nrow(d[d$body_name=="FALSG" & d$year>=1982 & d$year<=1989,])
round(deng_falsg / length(c(1982:1989))) #7 per year

##Hua-Deng Decision-Making
formal_huadeng <- nrow(d[d$body_name=="Politburo" & d$leader==1 & d$year>=1977 & d$year<=1981,])
informal_huadeng <- nrow(d[d$body_name=="Informal" & d$leader==1 & d$year>=1977 & d$year<=1981,])
round(informal_huadeng/(formal_huadeng+informal_huadeng),2) #76 percent

##Participation in Hua-Deng Decision-Making
both_huadeng <- nrow(d[d$attendee=="Both" & d$year>=1977 & d$year<=1981,])
diplomat_huadeng <- nrow(d[d$attendee=="Diplomat Only" & d$leader==1 & d$year>=1977 & d$year<=1981,])
military_huadeng <- nrow(d[d$attendee=="Military Only" &d$leader==1  & d$year>=1977 & d$year<=1981,])
round((both_huadeng + diplomat_huadeng) / (both_huadeng+diplomat_huadeng+military_huadeng),2) #21 percent diplomat
round(military_huadeng / (both_huadeng+diplomat_huadeng+military_huadeng),2) #79 percent

##Deng's Decision-Making
formal_deng <- nrow(d[d$body_name=="Politburo" & d$leader==1 & d$year>=1982 & d$year<=1989,])
informal_deng <- nrow(d[d$body_name=="Informal" &  d$leader==1 & d$year>=1982 & d$year<=1989,])
round(informal_deng/(formal_deng+informal_deng),2) #76 percent

##Participation in Deng's Decision-Making
both_deng <- nrow(d[d$attendee=="Both" & d$leader==1 & d$year>=1982 & d$year<=1987,])
diplomat_deng <- nrow(d[d$attendee=="Diplomat Only" & d$leader==1 & d$year>=1982 & d$year<=1989,])
military_deng <- nrow(d[d$attendee=="Military Only" & d$leader==1 & d$year>=1982 & d$year<=1989,])
round(both_deng / (both_deng+diplomat_deng+military_deng),2) #9 percent both or diplomat
round(diplomat_deng / (both_deng+diplomat_deng+military_deng),2) #39 percent both or diplomat
round(military_deng / (both_deng+diplomat_deng+military_deng),2) #52 percent military only
